<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>代码生成</title>
    <script src="../../../resources/app/all.js" type="text/javascript"></script>
    <style type="text/css">
        a:link {
            text-decoration: none;
        }

        a:visited {
            text-decoration: none;
        }

        a:hover {
            text-decoration: none;
        }

        a:active {
            text-decoration: none;
        }
    </style>

</head>
<body  class="easyui-layout" data-options="fit:true">
<div data-options="region:'center'" style="padding: 5px;">
    <form class="layui-form  layui-form-pane" id="genQueryForm" lay-filter="example">
        <div class="layui-inline">
            <label class="layui-form-label">表名称</label>
            <div class="layui-input-inline">
                <input id="tableName" name="tableName" class="layui-input" type="text" placeholder="请输入表名称"/>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">表描述</label>
            <div class="layui-input-inline">
                <input id="tableTitle" name="tableTitle" class="layui-input" type="text" placeholder="请输入表描述"/>
            </div>
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-sm" data-type="query">搜索</button>
            <button type="reset" class="layui-btn layui-btn-sm" >重置</button>
        </div>

    </form>
    <table class="layui-hide" id="genTable" lay-filter="genTable"></table>

</div>
<div data-options="region:'south',border:false">
    <div class="dialog-button">
        <button id="genSave" class="layui-btn layui-btn-normal">确定</button>
        <button id="genCancel" class="layui-btn layui-btn-primary">取消</button>
    </div>
</div>



<script>
    layui.use(['table'], function () {
        let table = layui.table;
        let renderTable = function (obj) {
            layer.load();
            ToolUtil.POST({
                url: global.api_url + "/sys/generator/tablePage",
                type: 'POST',
                data: JSON.stringify(obj),
                async: false,
                success: function (obj) {
                    if (obj && obj.code === '00') {
                        initData(obj.data);
                    } else if (obj.code === '98' || obj.code === '97') {
                        location.href = global.loginUrl;
                    } else {
                        layer.alert(obj.msg,{icon:5});
                    }
                }
            });

        };
        renderTable({});

        function initData(data) {
            table.render({
                elem: '#genTable',
                data: data.rows,
                defaultToolbar: [],
                title: '代码生成列表',
                cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'id',type: 'numbers', width: 80, title: 'ID', sort: true},
                    {field: 'tableName', title: '表名称'},
                    {field: 'tableDescribe', title: '表描述'},
                    {field: 'createTime', title: '创建时间', templet: function (d) {
                            return formatDateTime(new Date(d.createTime));
                        }},
                    {field: 'updateTime', title: '修改时间', templet: function (d) {
                            if(d.updateTime==null){
                                return '';
                            }
                            return formatDateTime(new Date(d.updateTime));
                        }},
                ]],
                page: {
                    layout: ['limit', 'limits', 'count', 'prev', 'page', 'next', 'skip'],
                    groups: 1,
                    first: false,
                    last: false,
                    limit:10,
                    limits: [10, 20, 30, 40, 50],
                    count: data.total
                },
                done: function (res, curr, count) {
                    layer.closeAll('loading');
                }
            });
        }

        table.on('checkbox(genTable)', function (obj) {
            let checkStatus = table.checkStatus('genTable');
            if (checkStatus.data.length == 1) {
                $("#editData").removeClass("layui-btn-disabled")
            } else {
                $("#editData").addClass("layui-btn-disabled")
            }
            if (checkStatus.data.length != 0) {
                $("#delData").removeClass("layui-btn-disabled")
            } else {
                $("#delData").addClass("layui-btn-disabled")
            }

        });


        let active = {
            query: function () {
                let data = ToolUtil.FormJSON("genQueryForm");
                layui.each(data, function (key, value) {
                    if (value == undefined || value == "") {
                        delete data[key];
                    }
                });
                renderTable(data);
            }
        };

        $('#genQueryForm .layui-btn').on('click', function () {
            let type = $(this).data('type');
            active[type].call(this);
        });


        layui.$('#genSave').on('click', function () {
            let checkStatus = table.checkStatus('genTable');
            let data = checkStatus.data;
            ToolUtil.POST({
                url: global.api_url+ "/sys/generator",
                data: JSON.stringify(data),
                async: false,
                success: function (obj) {
                    if (obj && obj.code === '00') {
                        saveClear();
                    } else if (obj.code === '98' || obj.code === '97') {
                        location.href = global.loginUrl;
                    } else {
                        layer.alert(obj.msg,{icon:5});
                    }
                }
            });
        });
        layui.$('#genCancel').on('click', function () {
            clear();
        });

        function saveClear() {
            parent.location.reload();
            clear();
        }
        function clear() {
            let index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
            layer.close(index);
        }



    });
</script>
</body>
</html>